package cn.ulyer.core.modules.system.service.impl;

import cn.ulyer.common.jpa.entity.QRolePermission;
import cn.ulyer.common.jpa.entity.RolePermission;
import cn.ulyer.common.jpa.service.AbstractService;
import cn.ulyer.core.modules.system.service.RolePermissionService;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.LinkedList;
import java.util.List;

@Service
public class RolePermissionServiceImpl extends AbstractService<RolePermission,Integer> implements RolePermissionService {


    @Override
    @Transactional
    public void updateRolePermissions(Integer roleId, List<Integer> permission) {
        jpaQueryFactory.delete(QRolePermission.rolePermission).where(QRolePermission.rolePermission.roleId.eq(roleId)).execute();
        if(permission!=null){
            List<RolePermission> all = new LinkedList<>();
            permission.forEach(p->{
                RolePermission rp = new RolePermission();
                rp.setPermissionId(p);
                rp.setRoleId(roleId);
                all.add(rp);
            });
            baseRepository.saveAll(all);
        }
    }

}
